Skip to content

[3/8] Megatron Bridge linear attention QAT and QAD example - #2657

Draft
kaix-nv wants to merge 13 commits into
kaix/linear-attention-decode-firstfrom
kaix/linear-attention-qat-example
Draft

kaix-nv wants to merge 13 commits into
kaix/linear-attention-decode-firstfrom
kaix/linear-attention-qat-example

Conversation

@kaix-nv

@kaix-nv kaix-nv commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Linear-attention series — 8 PRs

Order PR Depends on
1/8 #2497 GDN state/W QAT foundation main
2/8 #2519 GDN/KDA state QAT + INT8/FP8 #2497
3/8 #2657 Megatron Bridge QAT/QAD example #2519
4/8 #2805 Dynamic NVFP4 recurrent-state QAT #2657
5/8 #2788 ReplaySSM state QAT and two-precision design #2805
6/8 #2541 vLLM state-only fake quantization #2788
7/8 #2503 GDN/KDA prefill GEMM quantization #2541
8/8 #2507 Experimental approximate inverse #2503

The seven open PRs form native GitHub stack #2806 in this order. #2497 has landed, so #2519 targets main. #2805 contains NVFP4 and is based on #2657; #2788 and #2541 are rebased in sequence. #2503 and #2507 retain their existing commits; their rebases remain deferred until their immediate parents merge.

#2788 owns ReplaySSM and the planned two-precision design; the two-precision design is not implemented yet. #2541 owns vLLM engine integration. Serving-time prefill-GEMM QDQ remains deferred until an optimized fused kernel is available.

What does this PR do?

Type of change: New example.

Add a runnable Megatron Bridge example for recurrent-state QAT and QAD, using #2519's runtime. Bridge owns distributed scheduling, optimization and checkpoints. Both modes use pretrain(config, forward_step); QAD adds a frozen teacher through DistillationProvider.

For prompt/completion data, the example now derives each prefix from collator metadata (context_lengths and token_count, or explicit prefill_lengths and sequence_lengths). It never guesses prompts from loss-mask values. Right padding is excluded; custom packed batches supply per-segment prompt/valid lengths and storage boundaries. Token-only data retains the explicit fixed-split option. Multi-turn data must be rendered into a prompt and selected target continuation before tokenization.

Data-derived phases support TP/DP with PP=1 and CP=1. Fixed-length data retains the existing TP/PP/EP options and CP=1. TP ranks share the phase metadata. The adapter consumes a __next__-only iterator once, and applies the phase to the student only.

The dependency files document Bridge 056b78e / Core e998be07 and pin public vLLM 0.30.0. A KDA model additionally needs a compatible Bridge provider; the real-model workflow evidence is GDN.

Usage

Prompt/completion SFT data: a directory containing training.jsonl records with input and output fields. Without --prefill-tokens, the prompt lengths come from the tokenizer/collator.

bash examples/llm_qat/linear_attention/with_vllm_defaults.sh \
  torchrun --standalone --nproc-per-node=1 examples/llm_qat/linear_attention/train.py \
  --model /path/to/local-model --sft-data /path/to/prompt-data \
  --output /path/to/megatron-qat-checkpoint \
  --recipe general/ptq/linear_attention_state_int8_block32_dynamic \
  --train-steps 10 --length 128

Add --teacher-model /path/to/unquantized-model for QAD. For token-only indexed data, replace --sft-data with --train-data /path/to/train_text_document --prefill-tokens 64. See the example README for export/load instructions and STATE_QUANTIZATION.md for the prefix/decode mismatch and gradient design.

Testing

Rebased onto #2519 at 662fd69622; the parent-relative diff contains the example, tests and documentation.

  • DFW H100 job 20217113: QAT and QAD train/resume/export/HF load/Megatron reload tests passed, plus the new real Bridge prompt-derived QAD test. The job also tested the separate NVFP4 slice: four passed in 90.50s. The first attempt failed at single-rank test rendezvous after training; explicit file rendezvous fixed the test, and the complete workflow passed on rerun.
  • Focused CPU metadata test covers prompt lengths, right padding, packed boundaries and rejection of invalid/missing lengths. Changed-file pre-commit and diff checks passed.
  • Short full-model timing run, DFW 20216405: Qwen3.8-27B, four H100s, TP4/PP1/CP1, sequence128/prefix120/suffix8. Both old and new adapters completed six QAT and six QAD optimizer steps. Four measured-step medians: QAT 1.273→0.794s; QAD 1.201→0.993s. Timings vary and post-update losses differ, so this is throughput/workflow evidence only, not an accuracy or full-model numerical-equivalence claim. This run used the fixed-split entry point.
  • Earlier preserved-source GDN runs: ten steps each QAT/QAD on Qwen3.8-27B; save/resume, HF export/load and Megatron reimport passed. Those prior results do not substitute for new-source accuracy evaluation.

No KDA Bridge-provider, long-context quality, or full-engine serving claim is made. The chunk-boundary versus prefix/suffix training-strategy comparison is deferred.

Before your PR is "Ready for review"

  • Backward compatibility: additive example; runtime/policy changes belong to [2/8] GDN/KDA state QAT with native forwards and INT8/FP8 state #2519.
  • Dependencies/copying: imported Bridge/Core/FLA/vLLM APIs; optional dependency versions documented.
  • Necessary tests: shared-fixture QAT/QAD checkpoint round trips, one prompt-derived QAD test and one CPU metadata test.
  • Changelog: runtime feature entry belongs to the parent.
  • Review: draft; human/Claude review pending.

Additional Information

Step 3/8. State formats and execution are supplied by the parent; NVFP4 and ReplaySSM follow separately. Raw experiment records and the working design walkthrough remain outside the committed example.

@copy-pr-bot

copy-pr-bot Bot commented Oct 5, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@kaix-nv kaix-nv changed the title [2/2] Megatron Bridge linear attention QAT and QAD example Megatron Bridge linear attention QAT and QAD example (companion to #2519) Oct 5, 2026
@kaix-nv
kaix-nv added this pull request to stack #2563 October 5, 2026 06:11
@kaix-nv
kaix-nv removed this pull request from stack #2563 October 5, 2026 06:15
@kaix-nv
kaix-nv added this pull request to stack #2658 October 5, 2026 06:15
@kaix-nv kaix-nv changed the title Megatron Bridge linear attention QAT and QAD example (companion to #2519) [3/7] Megatron Bridge linear attention QAT and QAD example Oct 5, 2026
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from 2829885 to 3837d28 Compare October 6, 2026 17:27
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from 3837d28 to b7401f7 Compare October 6, 2026 19:55
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from b7401f7 to 0357915 Compare October 6, 2026 21:26
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from 0d898ac to 7717ea4 Compare October 8, 2026 05:35
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.37%. Comparing base (662fd69) to head (acdc5ab).

Additional details and impacted files
@@                         Coverage Diff                         @@
##           kaix/linear-attention-decode-first    #2657   +/-   ##
===================================================================
  Coverage                               68.37%   68.37%           
===================================================================
  Files                                     660      660           
  Lines                                   73810    73810           
===================================================================
  Hits                                    50464    50464           
  Misses                                  23346    23346           
Flag Coverage Δ
unit 59.69% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kaix-nv
kaix-nv removed this pull request from stack #2658 October 8, 2026 18:41
@kaix-nv
kaix-nv added this pull request to stack #2714 October 8, 2026 18:41
@kaix-nv kaix-nv changed the title [3/7] Megatron Bridge linear attention QAT and QAD example [3/6] Megatron Bridge linear attention QAT and QAD example Oct 8, 2026
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from 7717ea4 to 942115a Compare October 8, 2026 22:40
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch 2 times, most recently from 1dacb59 to 5731740 Compare October 11, 2026 06:22
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from 5731740 to 5de2761 Compare October 12, 2026 23:11
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch 2 times, most recently from 0b90b4f to 4db2574 Compare October 14, 2026 06:27
@kaix-nv
kaix-nv removed this pull request from stack #2714 October 14, 2026 06:28
@kaix-nv
kaix-nv added this pull request to stack #2789 October 14, 2026 06:29
@kaix-nv kaix-nv changed the title [3/6] Megatron Bridge linear attention QAT and QAD example [3/7] Megatron Bridge linear attention QAT and QAD example Oct 14, 2026
Signed-off-by: Kai Xu <kaix@nvidia.com>
Signed-off-by: Kai Xu <kaix@nvidia.com>
Update the linear-attention example for the flat execution config, native state-only training, and checkpoint migration. Remove the retired CPU reference script while retaining its historical results and source link.

Record the focused CPU, native GPU, Megatron checkpoint, and config migration validation. Documentation pre-commit hooks passed.

Signed-off-by: Kai Xu <kaix@nvidia.com>
Document the QDQ scheduling and arithmetic mismatch, the native prefix/handoff/suffix solution, gradient flow, and the limits of current validation. Align configuration guidance with the current state QAT API.

Signed-off-by: Kai Xu <kaix@nvidia.com>
Remove raw experiment JSON from the example and shorten the usage and state-alignment documents. Retain the mismatch derivation, supported policies, concise numerical results, and validation limits. Preserve development evidence in an untracked local archive.

Training code and minimal QAT/QAD tests are unchanged. Validation: scoped pre-commit, documentation links and anchors, Python snippet syntax, archive integrity, and git diff --check.

Signed-off-by: Kai Xu <kaix@nvidia.com>
Signed-off-by: Kai Xu <kaix@nvidia.com>
Signed-off-by: Kai Xu <kaix@nvidia.com>
Signed-off-by: Kai Xu <kaix@nvidia.com>
@kaix-nv
kaix-nv force-pushed the kaix/linear-attention-qat-example branch from 4db2574 to acdc5ab Compare October 14, 2026 20:17
@kaix-nv
kaix-nv removed this pull request from stack #2789 October 14, 2026 20:18
@kaix-nv
kaix-nv added this pull request to stack #2806 October 14, 2026 20:19
@kaix-nv kaix-nv changed the title [3/7] Megatron Bridge linear attention QAT and QAD example [3/8] Megatron Bridge linear attention QAT and QAD example Oct 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant